#include "netfinishedwidget.h"
#include "utils/util.h"

NetFinishedWidget::NetFinishedWidget(QWidget *parent)
    : QWidget{parent}
    , mainLayout(new QVBoxLayout(this))
    , HBoxLayout1(new QHBoxLayout())
    , HBoxLayout2(new QHBoxLayout())
    , VBoxLayout1(new QVBoxLayout())
    , netStatus(new QLabel())
    , netDescri(new QLabel())
    , labelIcon(new QLabel())
    , checkAgain(new QPushButton())
// , netWorkLabel1(nullptr)
// , netWorkLabel2(nullptr)
// , netWorkLabel3(nullptr)
// , netWorkLabel4(nullptr)
// , netWorkLabel5(nullptr)
// , netWorkPermissionIcon(nullptr)
// , networkConnectIcon(nullptr)
// , networkTcpConnectIcon(nullptr)
// , netWorkPermissionDescri(nullptr)
// , networkConnectDescirp(nullptr)
// , networkTcpConnectDescirp(nullptr)
// , networkResolveCost(nullptr)
// , networkConnectCost(nullptr)
{
    mainLayout->setObjectName("mainLayout");
    mainLayout->setContentsMargins(10, 10, 10, 10);
    mainLayout->setSpacing(6);
    mainLayout->setAlignment(Qt::AlignCenter);

    HBoxLayout1->setObjectName("HBoxLayout1");
    //初始化存放label的垂直布局，然后图标和网络状态文字也需要放在同一个水平布局中
    VBoxLayout1->setObjectName("VBoxLayout1");
    //垂直布局里面的水平布局
    HBoxLayout2->setObjectName("HBoxLayout2");
    HBoxLayout2->setSpacing(6);
    //然后就是将label放入到水平布局中
    labelIcon->setObjectName("labelIcon");
    labelIcon->setMinimumSize(QSize(20, 20));
    labelIcon->setMaximumSize(QSize(20, 20));
    QPixmap pixmap(":/resources/yes.png");
    pixmap = pixmap.scaled(labelIcon->size(), Qt::KeepAspectRatio);
    labelIcon->setPixmap(pixmap);
    labelIcon->setScaledContents(true);

    HBoxLayout2->addWidget(labelIcon);
    netStatus->setObjectName("netStatus");
    netStatus->setText("网络状态正常");
    QFont font = netStatus->font();
    setQFont(font, "Microsoft YaHe", 14, false);
    netStatus->setFont(font);
    HBoxLayout2->addWidget(netStatus);
    //将水平布局放入到垂直布局中去
    VBoxLayout1->addLayout(HBoxLayout2);

    netDescri->setObjectName("netDescri");
    netDescri->setText("完成网络检测，当前网络状况良好");
    setQFont(font, "Microsoft YaHe", 12, false);
    netDescri->setStyleSheet("QLabel{color:gray;}");
    VBoxLayout1->addWidget(netDescri);

    //将垂直布局放入到水平布局中
    HBoxLayout1->addLayout(VBoxLayout1);

    //需要加上一个spaceLabel
    QLabel *spaceLabel = new QLabel();
    spaceLabel->setObjectName("spaceLabel");
    spaceLabel->setMinimumSize(QSize(180, 40));
    spaceLabel->setMaximumSize(QSize(180, 40));
    HBoxLayout1->addWidget(spaceLabel);

    //重新检测按钮也需要加入到一个水平布局中
    QHBoxLayout *HBoxLayout3 = new QHBoxLayout();
    HBoxLayout3->setObjectName("HBoxLayout3");
    HBoxLayout3->setAlignment(Qt::AlignRight);
    checkAgain->setObjectName("checkAgain");
    checkAgain->setText("重新检测");
    checkAgain->setMinimumSize(80, 30);
    checkAgain->setMaximumSize(80, 30);
    checkAgain->setStyleSheet(
        "QPushButton{border:none; border-radius: 5px; background:rgb(0,111,255);color:white;}");
    HBoxLayout3->addWidget(checkAgain);

    //将这个按钮的水平布局，放入到上一栏的整体的水平布局中去
    HBoxLayout1->addLayout(HBoxLayout3);

    //水平布局需要加入到主布局中
    mainLayout->addLayout(HBoxLayout1);

    QHBoxLayout *lineHBoxLayout = new QHBoxLayout();
    lineHBoxLayout->setObjectName("lineHBoxLayout");
    lineHBoxLayout->setContentsMargins(0, 20, 0, 20);
    lineHBoxLayout->setAlignment(Qt::AlignCenter);
    //水平线
    QFrame *frame = new QFrame(this);
    frame->setFrameShape(QFrame::HLine);
    frame->setFrameShadow(QFrame::Sunken);
    frame->setFixedHeight(2); // 设置固定高度（比如 2 像素）
    frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    lineHBoxLayout->addWidget(frame);

    mainLayout->addLayout(lineHBoxLayout);
    // mainLayout->addWidget(frame);
}

/**
 * @brief NetFinishedWidget::setNetworkStatusDisplay
 * @param a  这是网络权限的标识符，如果为true说明正常
 * @param b 这是网络连接情况的标识符
 * @param c  这是网络服务器连接情况的标识符
 * @param d  这是域名解析耗时
 * @param e  这是连接耗时
 */
void NetFinishedWidget::setNetworkStatusDisplay(bool a, bool b, bool c, qint64 d, qint64 e)
{
    if (mainLayout->count() == 3) {
        QLayoutItem *item = mainLayout->takeAt(2);
        if (QLayout *layout = item->layout()) {
            if (layout->objectName() == "VBoxLayout2") {
                delete layout;
            }
        }
    }

    QVBoxLayout *VBoxLayout2 = new QVBoxLayout();
    VBoxLayout2->setContentsMargins(10, 10, 10, 10);
    VBoxLayout2->setObjectName("VBoxLayout2");
    VBoxLayout2->setSpacing(10);

    // 添加各个网络状态
    addNetworkStatus("网络权限", a, VBoxLayout2);
    addNetworkStatus("网络连接情况", b, VBoxLayout2);
    addNetworkStatus("服务器连接情况", c, VBoxLayout2);

    // 添加耗时状态
    addNetworkCost("域名解析耗时", d, VBoxLayout2);
    addNetworkCost("连接耗时", e, VBoxLayout2);

    // 增加空白的 label
    QLabel *spaceLabel = new QLabel();
    spaceLabel->setMinimumSize(QSize(170, 340));
    spaceLabel->setMaximumSize(QSize(170, 340));
    VBoxLayout2->addWidget(spaceLabel);

    mainLayout->addLayout(VBoxLayout2);
    // disconnect(checkAgain, &QPushButton::clicked, this, &NetFinishedWidget::onCheckAgainClicked);
    connect(checkAgain,
            &QPushButton::clicked,
            this,
            &NetFinishedWidget::onCheckAgainClicked,
            Qt::UniqueConnection);
}

void NetFinishedWidget::onCheckAgainClicked()
{
    emit checkAgainSignal();
}

void NetFinishedWidget::addNetWorkStatusLabel(QHBoxLayout *HBoxLayout,
                                              QLabel *label,
                                              QLabel *label1,
                                              QLabel *label2,
                                              bool status,
                                              const QString &text,
                                              const QString &yes_icon,
                                              const QString &no_icon)
{
    label->setAlignment(Qt::AlignLeft);
    label->setText(text);
    label->setMinimumSize(QSize(400, 20));
    label->setMaximumSize(QSize(400, 20));
    QFont font = label->font();
    setQFont(font, "Microsoft YaHe", 12, true);
    label->setFont(font);
    HBoxLayout->addWidget(label);

    if (status) {
        QPixmap pixmap(yes_icon);
        pixmap = pixmap.scaled(QSize(20, 20), Qt::KeepAspectRatio);
        label1->setPixmap(pixmap);
        label2->setText("正常");
        label2->setStyleSheet("QLabel{color:#379237; }");
    } else {
        QPixmap pixmap(no_icon);
        pixmap = pixmap.scaled(QSize(20, 20), Qt::KeepAspectRatio);
        label1->setPixmap(pixmap);
        label2->setText("错误");
        label2->setStyleSheet("QLabel{color:#F44336; }");
    }
    label1->setScaledContents(true);
    setQFont(font, "Microsoft YaHe", 12, true);
    label2->setFont(font);
    HBoxLayout->addWidget(label1);
    HBoxLayout->addWidget(label2);
    //颜色是#379237
    //#F44336 NO
}

void NetFinishedWidget::addNetWorkCostLabel(
    QHBoxLayout *HBoxLayout, QLabel *label, QLabel *label2, const qint64 &cost, const QString &text)
{
    label->setAlignment(Qt::AlignLeft);
    label->setText(text);
    label->setMinimumSize(QSize(400, 20));
    label->setMaximumSize(QSize(400, 20));
    QFont font = label->font();
    setQFont(font, "Microsoft YaHe", 12, true);
    label->setFont(font);
    HBoxLayout->addWidget(label);

    label2->setAlignment(Qt::AlignRight);
    // networkCostLabel->setMinimumSize(QSize(20, 20));
    // networkCostLabel->setMaximumSize(QSize(20, 20));
    if (cost == QINT64_MAX) {
        label2->setText("-9.9ms");
        label2->setStyleSheet("QLabel{color:#F44336;}");
    } else {
        QString cost_str = QString::number(cost);
        QString cost_text = cost_str + "ms";
        label2->setText(cost_text);
        label2->setStyleSheet("QLabel{color:#379237;}");
    }
    setQFont(font, "Microsoft YaHe", 12, false);
    label2->setFont(font);

    HBoxLayout->addWidget(label2);
}

void NetFinishedWidget::addNetworkStatus(const QString &statusDescription,
                                         bool status,
                                         QVBoxLayout *parentLayout)
{
    QHBoxLayout *hLayout = new QHBoxLayout();
    QLabel *label = new QLabel();
    QLabel *icon = new QLabel();
    QLabel *description = new QLabel();

    addNetWorkStatusLabel(hLayout,
                          label,
                          icon,
                          description,
                          status,
                          statusDescription,
                          ":/resources/yes2.png",
                          ":/resources/NO2.png");

    parentLayout->addLayout(hLayout);
}

void NetFinishedWidget::addNetworkCost(const QString &costDescription,
                                       qint64 cost,
                                       QVBoxLayout *parentLayout)
{
    QHBoxLayout *hLayout = new QHBoxLayout();
    QLabel *label = new QLabel();
    QLabel *costLabel = new QLabel();

    addNetWorkCostLabel(hLayout, label, costLabel, cost, costDescription);
    parentLayout->addLayout(hLayout);
}
